<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      chartInstance: null,
      //   allData: null, // 服务器返回的数据
      allData: [
        {
          name: "广东",
          value: 230
        },
        {
          name: "福建",
          value: 214
        },
        {
          name: "浙江",
          value: 203
        },
        {
          name: "上海",
          value: 310
        },
        {
          name: "北京",
          value: 289
        },
        {
          name: "江苏",
          value: 207
        },
        {
          name: "四川",
          value: 189
        },
        {
          name: "重庆",
          value: 195
        },
        {
          name: "陕西",
          value: 160
        },
        {
          name: "湖南",
          value: 140
        },
        {
          name: "河北",
          value: 170
        },
        {
          name: "辽宁",
          value: 106
        },
        {
          name: "湖北",
          value: 120
        },
        {
          name: "江西",
          value: 99
        },
        {
          name: "天津",
          value: 107
        },
        {
          name: "吉林",
          value: 143
        },
        {
          name: "青海",
          value: 65
        },
        {
          name: "山东",
          value: 166
        },
        {
          name: "山西",
          value: 134
        },
        {
          name: "云南",
          value: 87
        },
        {
          name: "安徽",
          value: 79
        }
      ],
      startValue: 0, //区域缩放的起点值
      endValue: 9, //区域缩放的终点值
      timerId: null //定时器的标识
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timerId);
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.rank_ref, this.theme);
      const initOption = {
        title: {
          text: "▍地区销售排行",
          left: 20,
          top: 12
        },
        grid: {
          top: "30%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          containLabel: true //距离是否包含坐标轴上的文字
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
          type: 'cross',
          label: {
            backgroundColor: '#6a7985'
          }
        }
        },
        xAxis: {
          type: "category"
        },
        yAxis: {
          type: "value"
        },
        series: [
          {
            type: "bar",
            label: {
              show: true,
              position: "top",
              textStyle: {
                color: "white",
                axisLabel: { interval: 0, rotate: 30 },
              }
            },
            itemStyle: {
                barBorderRadius: [33, 33, 0, 0],
            }
          }
        ]
      };
      this.chartInstance.setOption(initOption);
      //   对图标进行鼠标事件监听
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    async getData() {
      // 获取服务器的数据,对this.allData进行赋值后,调用updateChart方法更新图表
      //   const { data: res } = await this.$http.get("rank");
      //   this.allData = res;
      //   console.log(this.allData);
      this.allData.sort((a, b) => {
        return b.value - a.value;
      });
      this.updateChart();
      this.startInterval();
    },
    updateChart() {
      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E728F", "#23E5E5"],
        ["#5052EE", "#AB6EE5"]
      ];
      // 所有省份的数据
      const provinceArr = this.allData.map(item => {
        return item.name;
      });
      // 所有省份对应销售金额的数据
      const valueArr = this.allData.map(item => {
        return item.value;
      });
      const dataOption = {
        xAxis: {
          data: provinceArr
        },
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: arg => {
                let targetColorArr = null;
                if (arg.value > 300) {
                  targetColorArr = colorArr[0];
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1];
                } else {
                  targetColorArr = colorArr[2];
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0]
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1]
                  }
                ]);
              }
            }
          }
        ]
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0]
            }
          }
        ]
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updateChart();
      }, 2000);
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme(){
      this.chartInstance.dispose();//销毁当前的图表
      this.initChart(); // 重新以最新的主题名称初始化图表对象
      this.screenAdapter(); // 完成屏幕的适配
      this.updateChart(); // 更新图表的展示
    }
  }
};
</script>
<style scoped>
</style>
